package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OaInitiationContract;
import com.ruoyi.system.service.IOaInitiationContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 立项合同Controller
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@RestController
@RequestMapping("/system/contract")
@Transactional
public class OaInitiationContractController extends BaseController
{
    @Autowired
    private IOaInitiationContractService oaInitiationContractService;

    /**
     * 查询立项合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaInitiationContract oaInitiationContract)
    {
        startPage();
        List<OaInitiationContract> list = oaInitiationContractService.selectOaInitiationContractList(oaInitiationContract);
        return getDataTable(list);
    }

    /**
     * 导出立项合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "立项合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaInitiationContract oaInitiationContract)
    {
        List<OaInitiationContract> list = oaInitiationContractService.selectOaInitiationContractList(oaInitiationContract);
        ExcelUtil<OaInitiationContract> util = new ExcelUtil<OaInitiationContract>(OaInitiationContract.class);
        util.exportExcel(response, list, "立项合同数据");
    }

    /**
     * 获取立项合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaInitiationContractService.selectOaInitiationContractById(id));
    }
    //立项id查询
    @GetMapping(value = "/aaa")
    public AjaxResult getInfoA(String aaa)
    {
        OaInitiationContract oaInitiationContract = new OaInitiationContract();
        oaInitiationContract.setInitiationId(aaa);
        List<OaInitiationContract> list = oaInitiationContractService.selectOaInitiationContractList(oaInitiationContract);
        if (list.size()>=2){
         throw  new RuntimeException("该立项id有多个立项合同表");
        }
        if (list.isEmpty()){
         throw  new RuntimeException("当前立项没有合同 请新增");
        }
        return success(list.get(0));
    }
    /**
     * 新增立项合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "立项合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaInitiationContract oaInitiationContract)
    {
        return toAjax(oaInitiationContractService.insertOaInitiationContract(oaInitiationContract));
    }

    /**
     * 修改立项合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "立项合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaInitiationContract oaInitiationContract)
    {
        return toAjax(oaInitiationContractService.updateOaInitiationContract(oaInitiationContract));
    }

    /**
     * 删除立项合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "立项合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaInitiationContractService.deleteOaInitiationContractByIds(ids));
    }
}
